package org.millioncall.yueyoga.admin.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.millioncall.openplatform.common.PaginateParams;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.common.StartLimitPairImpl;
import org.millioncall.openplatform.dao.PreparedQuery;
import org.millioncall.yueyoga.admin.dao.AreaDAO;
import org.millioncall.yueyoga.admin.dao.BaseDao;
import org.millioncall.yueyoga.admin.model.Area;
import org.springframework.stereotype.Repository;

@Repository
public class AreaDAOImpl extends BaseDao implements AreaDAO {

	@Override
	public PaginatedList<Area> findPage(int start, int limit) {
		try {
			PreparedQuery<Area> pq = this.getPagableSqlSessionTemplate()
					.findAll("findAreaPage");
			PaginatedList<Area> pl = pq.list(new PaginateParams(
					new StartLimitPairImpl(start, limit)));
			return pl;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@Override
	public int update(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().insert("updateArea", map);
	}

	@Override
	public int save(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().insert("saveArea", map);
	}

	@Override
	public void deleteByIds(Integer[] deids) {
		Map<String, Object> map = new HashMap<String, Object>();
		for (int i = 0; i < deids.length; i++) {
			map.put("id", deids[i]);
			this.getPagableSqlSessionTemplate().delete("deleteArea", map);
		}
	}

	@Override
	public PaginatedList<Area> findPage(int start, int limit,
			Map<String, Object> map) {
		try {
			PreparedQuery<Area> pq = this.getPagableSqlSessionTemplate()
					.findAll("findAreaPages", map);
			PaginatedList<Area> pl = pq.list(new PaginateParams(
					new StartLimitPairImpl(start, limit)));
			return pl;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@Override
	public List<Area> findPage(Map<String, Object> map) {
		try {
			List<Area> pl = this.getPagableSqlSessionTemplate().selectList(
					"findAreaPage", map);
			return pl;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@Override
	public List<Area> findPage() {
		try {
			List<Area> pl = this.getPagableSqlSessionTemplate().selectList(
					"findAreaPage");
			return pl;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@Override
	public List<Area> findAreaByAreaId(Map<String, Object> map) {
		try {
			List<Area> pl = this.getPagableSqlSessionTemplate().selectList(
					"findAreaByAreaId", map);
			return pl;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@Override
	public Area findAreaById(Integer areaId) {
		return this.getPagableSqlSessionTemplate().selectOne("getAreaByID",
				areaId);
	}
}
